#include "usart.h"
#include "stdio.h"


static void usart_gpio_config(void)
{
	GPIO_InitTypeDef  gpio;
	RCC_APB2PeriphClockCmd(RCC_APB2ENR_IOPAEN, ENABLE);
	gpio.GPIO_Mode = GPIO_Mode_AF_PP;
	gpio.GPIO_Speed = GPIO_Speed_50MHz;
	gpio.GPIO_Pin = GPIO_Pin_9;
	GPIO_Init(GPIOA, &gpio);
	gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	gpio.GPIO_Speed = GPIO_Speed_50MHz;
	gpio.GPIO_Pin = GPIO_Pin_10;
	GPIO_Init(GPIOA, &gpio);  

}


//static void usart_nvic_cofig(void)
//{
//   NVIC_InitTypeDef  usart_nvic;
//   usart_nvic.NVIC_IRQChannel = USART1_IRQn;
//   usart_nvic.NVIC_IRQChannelCmd = ENABLE;
//   usart_nvic.NVIC_IRQChannelPreemptionPriority = 0;
//   usart_nvic.NVIC_IRQChannelSubPriority = 0;
//   NVIC_Init(&usart_nvic);
//}


static void usart_config(void)
{
 USART_InitTypeDef usart;

 RCC_APB2PeriphClockCmd(RCC_APB2ENR_USART1EN, ENABLE);
 	usart.USART_BaudRate = 115200;
	usart.USART_HardwareFlowControl = DISABLE;
	usart.USART_Mode = USART_Mode_Tx;
	usart.USART_Parity = USART_Parity_No;
	usart.USART_StopBits = USART_StopBits_1;
	usart.USART_WordLength = USART_WordLength_8b;
	USART_Init(USART1, &(usart));	
   USART_Cmd(USART1, ENABLE);
}

void usart_init(void)
{
 usart_gpio_config();
 //usart_nvic_cofig();
 usart_config();
}

void usart_putc(char c)
{
  USART_SendData(USART1,c);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)== RESET);
}

int fputc(int ch, FILE* stream)
{
  usart_putc(ch);
	return ch;
}
